package test;
import test.resoreceServer;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.xpath.XPathExpressionException;



import org.jdesktop.application.Application;
import org.xml.sax.SAXException;


public class properties extends javax.swing.JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jPanel1;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JButton jButton3;
	private JButton aceptar;
	private JButton jButton1;
	private JScrollPane tableServersDisponse;
	private JTable serverPriority;
	private JTextPane servidorDescripcion;
	private JButton jButton2;
	private JButton moveUp;
	private JButton addList;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JTextField sitioServidor;
	private JTextField nombreServidor;


	
	
	public properties(JFrame frame) {
		super(frame,"Propiedades",ModalityType.DOCUMENT_MODAL);
		initGUI();
		loadSofware();
	}
	
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			{
				jPanel1 = new JPanel();
				GroupLayout jPanel1Layout = new GroupLayout((JComponent)jPanel1);
				jPanel1.setLayout(jPanel1Layout);
				{
					nombreServidor = new JTextField();
				}
				{
					tableServersDisponse = new JScrollPane();
					{
						
						TableModel serverPriorityModel = 
								new DefaultTableModel(
										new String[][] { {"xgunz" ,"xgunz.com", "servidor oficial de FreeSofwareCenter" }, { "taringa", "taringa.com","servidor alternativo" },{"name","name.com","name"} },
										new String[] { "name", "host","description" });
						serverPriority = new JTable();
						tableServersDisponse.setViewportView(serverPriority);
						serverPriority.setModel(serverPriorityModel);
					}
				}
				{
					aceptar = new JButton();
					aceptar.setName("aceptar");
					aceptar.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							try {
								aceptarMouseClicked(evt);
							
								dispose();
								
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					});
				}
				{
					jButton3 = new JButton();
					jButton3.setName("jButton3");
					jButton3.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							System.out.println("jButton3.mouseClicked, event="+evt);
							//TODO add your code for jButton3.mouseClicked
							dispose();
						}
					});
				}
				{
					addList = new JButton();
					addList.setName("addList");
					addList.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							addListMouseClicked(evt);
						}
					});
				}
				{
					servidorDescripcion = new JTextPane();
					servidorDescripcion.setName("servidorDescripcion");
				}
				{
					jButton1 = new JButton();
					jButton1.setName("jButton1");
					jButton1.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jButton1MouseClicked(evt);
						}
					});
				}
				{
					jLabel4 = new JLabel();
					jLabel4.setName("jLabel4");
				}
				{
					moveUp = new JButton();
					moveUp.setName("moveUp");
					moveUp.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							moveUpMouseClicked(evt);
						}
					});
				}
				{
					jButton2 = new JButton();
					jButton2.setName("jButton2");
					jButton2.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							jButton2MouseClicked(evt);
						}
					});
				}
				{
					sitioServidor = new JTextField();
				}
				{
					jLabel2 = new JLabel();
					jLabel2.setName("jLabel2");
				}
				{
					jLabel3 = new JLabel();
					jLabel3.setName("jLabel3");
				}
				{
					jLabel1 = new JLabel();
					jLabel1.setName("jLabel1");
				}
				jPanel1Layout.setVerticalGroup(jPanel1Layout.createSequentialGroup()
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(jLabel2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jLabel1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(addList, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(sitioServidor, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					    .addComponent(nombreServidor, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(jLabel3, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					.addComponent(servidorDescripcion, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
					.addGap(27)
					.addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addComponent(tableServersDisponse, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 268, GroupLayout.PREFERRED_SIZE)
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addGap(21)
					        .addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)
					        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					        .addComponent(moveUp, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					        .addGap(15)
					        .addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
					        .addGap(105)))
					.addGap(83, 83, GroupLayout.PREFERRED_SIZE)
					.addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
					    .addComponent(aceptar, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE)
					    .addComponent(jButton3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 43, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(26, 26));
				jPanel1Layout.setHorizontalGroup(jPanel1Layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(jPanel1Layout.createParallelGroup()
					    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					        .addGroup(jPanel1Layout.createParallelGroup()
					            .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					                .addComponent(aceptar, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
					                .addGap(20))
					            .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					                .addComponent(jLabel3, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
					                .addGap(33)))
					        .addGap(7)
					        .addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
					        .addGap(72)
					        .addComponent(jButton3, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
					        .addGap(0, 75, Short.MAX_VALUE))
					    .addGroup(jPanel1Layout.createSequentialGroup()
					        .addGroup(jPanel1Layout.createParallelGroup()
					            .addGroup(jPanel1Layout.createSequentialGroup()
					                .addGap(0, 0, Short.MAX_VALUE)
					                .addComponent(tableServersDisponse, GroupLayout.PREFERRED_SIZE, 344, GroupLayout.PREFERRED_SIZE))
					            .addGroup(jPanel1Layout.createSequentialGroup()
					                .addGroup(jPanel1Layout.createParallelGroup()
					                    .addComponent(nombreServidor, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
					                    .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					                        .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 152, GroupLayout.PREFERRED_SIZE)
					                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)))
					                .addGap(29)
					                .addGroup(jPanel1Layout.createParallelGroup()
					                    .addComponent(sitioServidor, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
					                    .addComponent(jLabel2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE))
					                .addGap(0, 0, Short.MAX_VALUE))
					            .addGroup(jPanel1Layout.createSequentialGroup()
					                .addComponent(servidorDescripcion, GroupLayout.PREFERRED_SIZE, 344, GroupLayout.PREFERRED_SIZE)
					                .addGap(0, 0, Short.MAX_VALUE)))
					        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
					        .addGroup(jPanel1Layout.createParallelGroup()
					            .addComponent(jButton1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
					            .addComponent(moveUp, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
					            .addComponent(jButton2, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
					            .addGroup(GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
					                .addComponent(addList, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
					                .addGap(39)))))
					.addContainerGap());
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addComponent(jPanel1, 0, 604, Short.MAX_VALUE));
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addComponent(jPanel1, 0, 505, Short.MAX_VALUE));
			this.setSize(512, 680);
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(getContentPane());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
private void loadSofware(){
	try {
		resoreceServer.getListServer(serverPriority);
	} catch (XPathExpressionException | ParserConfigurationException
			| SAXException | IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	}

	private void addListMouseClicked(MouseEvent evt) {
		System.out.println("addList.mouseClicked, event="+evt);
		//TODO add your code for addList.mouseClicked
		

		
		
		String[] t= new String[3];
		t[0]=nombreServidor.getText();
		t[1]=sitioServidor.getText();
		t[2]=servidorDescripcion.getText();
		
		if(!(t[0].equals("")) && !(t[1].equals("")) && !(t[2].equals("")))
		{
			
			
			// testea que conecta con el servidor
			
			if(resoreceServer.constatarServidor(t[1]))
			{	
				DefaultTableModel model = (DefaultTableModel) serverPriority.getModel();
				model.addRow(t);
			}
			else 
			{
				System.out.println("error conecto al servidor");
			}
			
			
			
			
		}
		else 
		{
			
			  JOptionPane.showMessageDialog(null,
					    "los campos no pueden estar vacios",
					    "Error campos vacios",
					    JOptionPane.ERROR_MESSAGE);	
		}
	
		

	}
	
	private void moveUpMouseClicked(MouseEvent evt) {
		System.out.println("moveUp.mouseClicked, event="+evt);
		//TODO add your code for moveUp.mouseClicked
		
		
		int[] seleccionado=serverPriority.getSelectedRows();
		DefaultTableModel model = (DefaultTableModel) serverPriority.getModel();
		
		int i=0;
		for ( i=0;i<seleccionado.length;i++)
		{
			if(seleccionado[i]-1>-1)
				model.moveRow(seleccionado[i], seleccionado[i], seleccionado[i]-1);
		}
		
		
	}
	
	private void jButton2MouseClicked(MouseEvent evt) {
		System.out.println("jButton2.mouseClicked, event="+evt);
		//TODO add your code for jButton2.mouseClicked
		
		int[] seleccionado=serverPriority.getSelectedRows();
		DefaultTableModel model = (DefaultTableModel) serverPriority.getModel();
		
		int i=0;
		for ( i=0;i<seleccionado.length;i++)
		{
			
			if(seleccionado[i]+1<serverPriority.getRowCount())
				model.moveRow(seleccionado[i], seleccionado[i], seleccionado[i]+1);
		}
	}
	
	private void aceptarMouseClicked(MouseEvent evt) throws Exception {
		System.out.println("aceptar.mouseClicked, event="+evt);
		//TODO add your code for aceptar.mouseClicked
		resoreceServer.setListServer(serverPriority);
	}
	
	private void jButton1MouseClicked(MouseEvent evt) {
		System.out.println("jButton1.mouseClicked, event="+evt);
		//TODO add your code for jButton1.mouseClicked
		
		
		int[] seleccionado=serverPriority.getSelectedRows();
		DefaultTableModel model = (DefaultTableModel) serverPriority.getModel();
		
		int i=0;
		for ( i=0;i<seleccionado.length;i++)
		{
			if(seleccionado[i]-1>-1)
				model.removeRow(seleccionado[i]);
		}
	}

}
